/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Feb 1, 2011
 * @Createtime 2:00:52 AM
 */

package com.tmh.study.aASA
{
	import flash.display.Sprite;
	import flash.events.Event;

	public class SteeredVehicleTest extends Sprite
	{
		private var _vehicleA:SteeredVehicle;

		private var _vehicleB:SteeredVehicle;

		private var _vehicleC:SteeredVehicle;

		private var _circles:Array;

		private var _path:Array;

		private var _numCircles:int = 10;

		public function SteeredVehicleTest()
		{
			_circles = new Array();

			_path = new Array();

			for (var i:int = 0; i < _numCircles; i++)
			{
				var circle:Circle = new Circle(20);
				circle.x = Math.random() * stage.stageWidth;
				circle.y = Math.random() * stage.stageHeight;

				addChild(circle);

				_circles.push(circle);

				_path.push(new Vector2D(circle.x, circle.y));
			}

			_vehicleA = new SteeredVehicle();
			_vehicleA.position = new Vector2D(100, 100);
			_vehicleA.maxSpeed = 2;
			_vehicleA.mass = 2;
			_vehicleA.edgeBehavior = Vehicle.BOUNCE;

			_vehicleB = new SteeredVehicle();
			_vehicleB.position = new Vector2D(400, 400);
			_vehicleB.maxSpeed = 3;
			_vehicleB.mass = 3;
			_vehicleB.edgeBehavior = Vehicle.BOUNCE;

			_vehicleC = new SteeredVehicle();
			_vehicleC.position = new Vector2D(700, 100);
			_vehicleC.maxSpeed = 4;
			_vehicleC.mass = 4;
			_vehicleC.edgeBehavior = Vehicle.BOUNCE;

			addChild(_vehicleA);
			addChild(_vehicleB);
			addChild(_vehicleC);
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}

		private function enterFrameHandler(event:Event):void
		{
			_vehicleA.seek(_vehicleC.position);
			_vehicleA.avoid(_circles);
			//_vehicleA.evade(_vehicleB);
			//_vehicleA.wander();

			_vehicleB.pursue(_vehicleC);
			_vehicleB.avoid(_circles);
			//_vehicleB.evade(_vehicleA);
			//_vehicleB.wander();

			//_vehicleC.arrive(new Vector2D(mouseX, mouseY));
			//_vehicleC.evade(_vehicleB);
			//_vehicleC.evade(_vehicleA);
			//_vehicleC.avoid(_circles);
			//_vehicleC.wander();
			_vehicleC.followPath(_path, true);

			_vehicleA.update();
			_vehicleB.update();
			_vehicleC.update();
		}
	}
}